
clear all
set more off

include "bill-analysis/bill_setup.do"

// two year groups
gen byte even_yr = mod(year, 2) == 0
bys fips (year): gen byte yrg = sum(even_yr) if year >= 1990
assert !mi(yrg) `rng'

xtset fips year

collapse (sum) `billdv' `lawdv' laws_tighten laws_loosen      /*
    */         totshoot=L.shoot totfatal=L.`fatal' totnews=L.news       /*
    */  (mean) shoot=L.shoot fatal=L.`fatal' shootX???leg fatalX???leg  /*
    */         pc_fatal=L.pc_`fatal' murder=L.`murder' news=L.news      /*
    */         demleg repleg repgov had_reg_session biennium            /*
    */         `demo' `rng', by(fips usps yrg) fast

loc gov = subinstr("`gov'", "i.", "", .)
di "Institutional controls: `gov'"

// columns 3 and 7 of table 2 and table C1
foreach dv of varlist `billdv' `lawdv' {

    eststo clear
    loc m = 1

    foreach iv of varlist shoot fatal {

        xtpoisson `dv' `iv' `gov' `politics' i.yrg, `opt'
        estimates store `dv'_`iv'
        qui estadd local sfix = "Yes"
        qui estadd local yfix = "Yes"
        qui estadd local trnd = "No"
        eststo m`m' , title("Politics")
        loc m = `m' + 1

        xtpoisson `dv' `iv' `gov' `politics' `demo' i.yrg, `opt'
        qui estadd local sfix = "Yes"
        qui estadd local yfix = "Yes"
        qui estadd local trnd = "No"
        eststo m`m' , title("Demo")
        loc m = `m' + 1
    }

    // interaction with politics
    foreach v in shoot fatal {

        xtpoisson `dv' `v'X???leg `gov' `politics' i.yrg, `opt'
        estimates store `dv'_`iv'_xpol
        qui estadd local sfix = "Yes"
        qui estadd local yfix = "Yes"
        qui estadd local trnd = "No"
        eststo m`m' , title("X Pol")
        loc m = `m' + 1

        xtpoisson `dv' `v'X???leg `gov' `politics' `demo' i.yrg, `opt'
        qui estadd local sfix = "Yes"
        qui estadd local yfix = "Yes"
        qui estadd local trnd = "No"
        eststo m`m' , title("X Pol Demo")
        loc m = `m' + 1
    }

    writemodel using "`out'/yrg_`dv'", ext(csv) order(shoot fatal /*
        */ shootX* fatalX* had_reg_session biennium)
}


// loosening and tightening

foreach dv of varlist laws_tighten laws_loosen {

    eststo clear
    loc m = 1

    foreach v in shoot fatal {
        xtpoisson `dv' `v' `gov' `politics' i.yrg, `opt'
        estimates store `dv'_`iv'
        qui estadd local sfix = "Yes"
        qui estadd local yfix = "Yes"
        qui estadd local trnd = "No"
        eststo m`m' , title("Politics")
        loc m = `m' + 1

        xtpoisson `dv' `v' `gov' `politics' `demo' i.yrg, `opt'
        qui estadd local sfix = "Yes"
        qui estadd local yfix = "Yes"
        qui estadd local trnd = "No"
        eststo m`m' , title("Demo")
        loc m = `m' + 1

        xtpoisson `dv' `v'X???leg `gov' `politics' i.yrg, `opt'
        estimates store `dv'_`iv'_xpol
        qui estadd local sfix = "Yes"
        qui estadd local yfix = "Yes"
        qui estadd local trnd = "No"
        eststo m`m' , title("X Pol")
        loc m = `m' + 1

        xtpoisson `dv' `v'X???leg `gov' `politics' `demo' i.yrg, `opt'
        qui estadd local sfix = "Yes"
        qui estadd local yfix = "Yes"
        qui estadd local trnd = "No"
        eststo m`m' , title("X Pol Demo")
        loc m = `m' + 1
    }

    writemodel using "`out'/yrg_`dv'", ext(csv) order(shoot fatal /*
        */ shootX* fatalX* had_reg_session biennium)
}


// compare mass shootings with murder (column 3 of table 3)

loc iv "pc_fatal murder"
eststo clear

xtpoisson `billdv' `iv' `gov' `politics' i.yrg, `opt'
qui estadd local sfix = "Yes"
qui estadd local yfix = "Yes"
qui estadd local trnd = "No"
eststo m1, title("Politics")

xtpoisson `billdv' `iv' `gov' `politics' `demo' i.yrg, `opt'
qui estadd local sfix = "Yes"
qui estadd local yfix = "Yes"
qui estadd local trnd = "No"
eststo m2, title("Demo")

writemodel using "`out'/yrg_murder", ext(csv) /*
    */ order(pc_fatal murder had_reg_session biennium)


// media coverage of mass shootings (column 3 of table 6)

loc iv "totnews shoot"
eststo clear

xtpoisson `billdv' `iv' `gov' `politics' i.yrg, `opt'
qui estadd local sfix = "Yes"
qui estadd local yfix = "Yes"
qui estadd local trnd = "No"
eststo m1, title("Politics")

xtpoisson `billdv' `iv' `gov' `politics' `demo' i.yrg, `opt'
qui estadd local sfix = "Yes"
qui estadd local yfix = "Yes"
qui estadd local trnd = "No"
eststo m2, title("Demo")

writemodel using "`out'/yrg_news", ext(csv) /*
    */ order(totnews shoot had_reg_session biennium)


estimates restore `billdv'_shoot
cap count if shoot != 0
loc tot = r(N)
cap count if shoot == 0.5
loc hlf = r(N)
cap count if shoot == 1
loc all = r(N)

lincom _b[shoot]*(`hlf'/`tot')*0.5 + _b[shoot]*(`all'/`tot')
loc est = r(estimate)

sum `billdv', meanonly
loc effect_size = round(`est' * r(mean), 0.1)
di as result "Shooting results in `effect_size' more bills for average state."

exit
